package com.cunjin.student.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cunjin.base.service.impl.BaseServiceImpl;
import com.cunjin.dormitory.dao.DormitoryDao;
import com.cunjin.dormitory.entity.DormitoryEntity;
import com.cunjin.grade.dao.GradeDao;
import com.cunjin.grade.entity.GradeEntity;
import com.cunjin.student.dao.StudentDao;
import com.cunjin.student.entity.StudentEntity;
import com.cunjin.student.service.StudentService;

import tk.mybatis.mapper.entity.Example;

@Service
public class StudentServiceImpl extends BaseServiceImpl<StudentEntity> implements StudentService {

	
	@Autowired
	private StudentDao studentDao;
	
	@Autowired
	private DormitoryDao dormitoryDao;
	
	@Autowired
	private GradeDao gradeDao;
	
	@Transactional(readOnly=true)
	@Override
	public Map<String, Object> login(StudentEntity entity,HttpServletRequest req) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		StudentEntity user;
		try {
			user = studentDao.login(entity);
			if(null !=user){
				HttpSession session = req.getSession();
				String loginNo = entity.getLoginNo();
				String username = user.getName();
				session.setAttribute("loginNo", loginNo);
				session.setAttribute("username", username);
				map.put("code", "200");
				map.put("jg", "登陆成功");
			}else{
				map.put("code", "500");
				map.put("jg", "登陆失败");
			}
		} catch (Exception e) {
			map.put("code", "500");
			map.put("jg", "登陆失败");
			e.printStackTrace();
		}
		
		return map;
	}
	
	@Transactional(readOnly=true)
	@Override
	public Map<String, Object> queryByloginNo(StudentEntity entity) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		StudentEntity user;
		try {
			user = studentDao.queryByloginNo(entity);
			if(null ==user){
				map.put("code", "200");
				map.put("jg", "ok");
			}else{
				map.put("code", "500");
				map.put("jg", "no");
			}
		} catch (Exception e) {
			map.put("code", "500");
			map.put("jg", "no");
			e.printStackTrace();
		}
		return map;
	}

	
	@Transactional(readOnly=false)
	@Override
	public Map<String, Object> insertStudent(StudentEntity entity) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			int dormitoryId = entity.getDormitoryId();
			
			//拿到宿舍的id
			DormitoryEntity dor = dormitoryDao.selectByPrimaryKey(dormitoryId);
			
			//将宿舍的现在的人数加一
			Integer nowNum = dor.getNowNum();
			nowNum+=1;
			dor.setNowNum(nowNum);
			dormitoryDao.updateByPrimaryKeySelective(dor);
			
			int i = studentDao.insertStudent(entity);
			if(1==i){
				map.put("code", "200");
				map.put("jg", "ok");
			}else{
				map.put("code", "500");
				map.put("jg", "no");
			}
		} catch (Exception e) {
			map.put("code", "500");
			map.put("jg", "no");
			e.printStackTrace();
		}
		return map;
	}
	
	@Transactional(readOnly=false)
	@Override
	public Map<String, Object> queryByPage(Map<String, Object> pages) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<StudentEntity> ls=studentDao.myqueryByPage(pages);
			map.put("total",studentDao.findAllByName(pages));
			map.put("rows",ls);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 分页下添加一个学生
	 */
	@Transactional(readOnly=false)
	@Override
	public Map<String, Object> addStudent(StudentEntity entity) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		//拿到宿舍名称，添加到具体宿舍
		DormitoryEntity dormitoryEntity=null;
		GradeEntity gradeEntity =null;
		String dormitoryName = entity.getDormitoryName();
		
		try {
			dormitoryEntity =dormitoryDao.selectByName(dormitoryName);
			
			//拿到现在的人数
			Integer nowNum = dormitoryEntity.getNowNum();
			nowNum=nowNum+1;
			dormitoryEntity.setNowNum(nowNum);
			dormitoryDao.updateByPrimaryKeySelective(dormitoryEntity);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		//查找到学生的年级id
		String grateName = entity.getGrateName();
		try {
			gradeEntity = gradeDao.selectByName(grateName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		int gradeid = gradeEntity.getId();
		
		int dormitoryId = dormitoryEntity.getId();
		entity.setDormitoryId(dormitoryId);
		entity.setGrateId(gradeid);
		
		//判断是否有这个学生
		String loginNo = entity.getLoginNo();
		Example example = new Example(StudentEntity.class); 
		example.createCriteria().andCondition("login_no="+loginNo);
		List<StudentEntity> selectByExample = studentDao.selectByExample(example);
		int size = selectByExample.size();
		//==0说明不存在这个学生
		if(size==0){
			
				int i = studentDao.insertUseGeneratedKeys(entity);
				if(1==i){
					map.put("code", 200);
					map.put("msg", "添加成功");
				}else{
					map.put("code", 500);
					map.put("msg", "添加失败");
				}
				
				//添加到学生表中
				return map;
			}else{
				map.put("code", 303);
				map.put("msg", "该学号已经存在");
				return map;
			}
		}

	
	@SuppressWarnings("unused")
	@Transactional(readOnly=false)
	@Override
	public Map<String, Object> deleteByids(String[] split) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			
			int cs=0;
			for (String s : split) {
				int sid=Integer.parseInt(s);
				StudentEntity e = studentDao.selectByPrimaryKey(sid);
				Integer dormitoryId = e.getDormitoryId();
				int oo=dormitoryDao.updateNownumByid(dormitoryId);
				cs++;
			}
			int ls=studentDao.deleteByids(split);
			
			if(ls==cs){
				map.put("count",ls);
				map.put("code",200);
				
			}
		} catch (Exception e) {
			map.put("code",500);
			e.printStackTrace();
		}
		return map;
	}

	@SuppressWarnings("unused")
	@Transactional(readOnly=false)
	@Override
	public Map<String, Object> updateStudent(StudentEntity entity) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
			DormitoryEntity dormitoryEntity=null;
			GradeEntity gradeEntity =null;
			
			
			try {
				String dormitoryName = entity.getDormitoryName();
				//拿到以前的宿舍name,根据以前的宿舍id拿
				Integer id = entity.getId();
				//拿还没修改前的学生 拿到宿舍id
				StudentEntity oldstu = studentDao.selectByPrimaryKey(id);
				//旧的宿舍id
				Integer olddormi = oldstu.getDormitoryId();
				Integer lodgrateid = oldstu.getGrateId();
				

				//查找到学生的年级id
				//修改班级
				String grateName = entity.getGrateName();
				gradeEntity = gradeDao.selectByName(grateName);
				int newgradeid = gradeEntity.getId();
				
				entity.setGrateId(newgradeid);
				
				//判断是否修改了班级
				
				
				
				dormitoryEntity =dormitoryDao.selectByName(dormitoryName);
				//新的宿舍id
				Integer newdormi = dormitoryEntity.getId();
				
				//修改了宿舍的情况
				if(olddormi !=newdormi){
					//拿到现在的人数
					Integer nowNum = dormitoryEntity.getNowNum();
					nowNum=nowNum+1;
					dormitoryEntity.setNowNum(nowNum);
					dormitoryDao.updateByPrimaryKeySelective(dormitoryEntity);
					entity.setDormitoryId(newdormi);
					
					//旧宿舍人数减一
					DormitoryEntity oldDor = dormitoryDao.selectByPrimaryKey(olddormi);
					Integer olnum = oldDor.getNowNum();
					olnum=olnum-1;
					oldDor.setNowNum(olnum);
					dormitoryDao.updateByPrimaryKeySelective(oldDor);
				}
				//更新学生
				
				//判断是否有这个学生
				String loginNo = entity.getLoginNo();
				Example example = new Example(StudentEntity.class); 
				example.createCriteria().andCondition("login_no="+loginNo);
				List<StudentEntity> selectByExample = studentDao.selectByExample(example);
				int size = selectByExample.size();
				//==0说明不存在这个学生
				if(size==1){
					
						int i = studentDao.updateByPrimaryKeySelective(entity);
						if(1==i){
							map.put("code", 200);
							map.put("msg", "添加成功");
						}else{
							map.put("code", 500);
							map.put("msg", "添加失败");
						}
						
						//添加到学生表中
						return map;
					}else{
						map.put("code", 303);
						map.put("msg", "该学号已经存在");
						return map;
					}
			
				
			} catch (Exception e1) {
				
				e1.printStackTrace();
				map.put("code", 303);
				map.put("msg", "更新失败");
				return map;
			}

				
	}
			
			
		

	
	
}
